package com.example.content.viewmodule.product

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.example.content.repository.ApiResult
import com.example.content.repository.RepositoryMgr
import com.example.content.repository.bean.CreateProductReq
import com.example.content.repository.bean.ProductInfo
import com.example.content.repository.files.FileConvert
import com.example.content.viewmodule.BaseViewModel
import com.example.content.viewmodule.viewdata.CommonViewState
import com.example.core.livedata.SingleLiveEvent
import com.example.core.network.body.ProgressResponseCallback

/**
 * Created by zhudongao
 * Desc
 * Date 2020/11/7
 */
class UpProductViewModel: BaseViewModel<CommonViewState>() {

    val resultData = SingleLiveEvent<ApiResult<ProductInfo>>()
    val percentData = MutableLiveData<String>("")

    fun uploadPhotos(msg:String, files: List<String>, resourceType:Int, gender:Int){
        launch(
            block = {
                val imageList = RepositoryMgr.uploadFile(FileConvert.fileWrappers(files, object :
                    ProgressResponseCallback {
                    override fun onResponseProgress(
                        bytesWritten: Long,
                        contentLength: Long,
                        finish: Boolean
                    ) {
                        Log.e(
                            "jindujianting",
                            "百分比：${(bytesWritten.toFloat() / contentLength) * 100}%, 写入:${bytesWritten}, 总共:${contentLength}"
                        )
                        percentData.postValue("${(bytesWritten.toFloat() / contentLength) * 100}%")
                    }
                }))
                val reqInfo = CreateProductReq(
                    "南京市", msg, gender, imageList?.get(0), resourceType, false,
                    0, imageList
                )
                resultData.postValue(RepositoryMgr.uploadProduct(reqInfo))
            }
        )
    }
}